home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 3 / Light ROM 3 - Disc 2.iso / programs / amiga / rgb2ire / rgb2ire.c < prev   
C/C++ Source or Header  |  1995-01-31  |  6KB  |  267 lines

  1. /*
  2. ======================================================================
  3. rgb2ire.c
  4. Ernie Wright  30 Jan 95
  5. SAS/C 6.51
  6.  
  7. Display NTSC composite signal amplitude in IRE, given RGB.
  8. ====================================================================== */
  9.  
  10. #include <proto/exec.h>
  11. #include <proto/intuition.h>
  12. #include <proto/graphics.h>
  13. #include <proto/gadtools.h>
  14. #include <intuition/gadgetclass.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <math.h>
  19.  
  20.  
  21. int ire( int red, int green, int blue );
  22. BOOL user( void );
  23.  
  24.  
  25. void main( void )
  26. {
  27.    user();
  28. }
  29.  
  30.  
  31. /*
  32. ======================================================================
  33. ire()
  34.  
  35. Calculate NTSC composite signal amplitude in IRE, given RGB.
  36.  
  37. INPUTS
  38.    red, green, blue     levels in [0, 255]
  39.  
  40. RESULTS
  41.    Returns the composite signal amplitude.
  42.  
  43. The inputs are scaled into [0.0, 1.0] and gamma corrected.  YIQ is
  44. found as a linear transformation using the standard matrix.  The
  45. resulting amplitude is converted to IRE units.  Note that as Y goes
  46. from 0.0 to 1.0 here, IRE level goes from 7.5 (the NTSC pedestal) to
  47. 100.0.
  48.  
  49. Based on "Television Color Encoding and 'Hot' Broadcast Colors" by
  50. David Martindale and Alan W. Paeth, in GRAPHICS GEMS II, James Arvo
  51. ed., Academic Press, 1991.
  52. ====================================================================== */
  53.  
  54. int ire( int red, int green, int blue )
  55. {
  56.    double r, g, b, y, i, q, a;
  57.  
  58.    r = pow(( double ) red   / 255.0, 0.45 );
  59.    g = pow(( double ) green / 255.0, 0.45 );
  60.    b = pow(( double ) blue  / 255.0, 0.45 );
  61.    y = 0.2989 * r + 0.5866 * g + 0.1144 * b;
  62.    i = 0.5959 * r - 0.2741 * g - 0.3218 * b;
  63.    q = 0.2113 * r - 0.5227 * g + 0.3113 * b;
  64.    a = y + sqrt( i * i + q * q );
  65.  
  66.    return ( int )( 8.0 + 92.5 * a );  /* the extra 0.5 is for rounding */
  67. }
  68.  
  69.  
  70. static BOOL open_window( void );
  71. static void close_window( void );
  72. static void set_level( void );
  73. static void draw_box( void );
  74.  
  75. struct Window *wnd;
  76. struct TextAttr myfont = { "topaz.font", 8, 0, 0 };
  77. APTR visinfo;
  78. struct Gadget *glist, *gad[ 4 ];
  79. int top, rgb[ 3 ], entry3[ 3 ];
  80.  
  81.  
  82. BOOL user( void )
  83. {
  84.    struct IntuiMessage msg, *pmsg;
  85.    struct Gadget *g;
  86.    BOOL done = FALSE;
  87.  
  88.  
  89.    if ( !open_window() ) return FALSE;
  90.  
  91.    while ( !done ) {
  92.       pmsg = GT_GetIMsg( wnd->UserPort );
  93.       if ( !pmsg ) {
  94.          WaitPort( wnd->UserPort );
  95.          continue;
  96.       }
  97.       memcpy( &msg, pmsg, sizeof( struct IntuiMessage ));
  98.       GT_ReplyIMsg( pmsg );
  99.       g = ( struct Gadget * ) msg.IAddress;
  100.  
  101.       switch ( msg.Class )
  102.       {
  103.          case IDCMP_REFRESHWINDOW:
  104.             GT_BeginRefresh( wnd );
  105.             draw_box();
  106.             GT_EndRefresh( wnd, TRUE );
  107.             break;
  108.  
  109.          case IDCMP_CLOSEWINDOW:
  110.             done = TRUE;
  111.             break;
  112.  
  113.          case IDCMP_MOUSEMOVE:
  114.             switch ( g->GadgetID ) {
  115.                case 1:
  116.                case 2:
  117.                case 3:
  118.                   rgb[ g->GadgetID - 1 ] = msg.Code;
  119.                   set_level();
  120.                   break;
  121.                default:
  122.                   break;
  123.             }
  124.  
  125.          default:
  126.             break;
  127.       }
  128.    }
  129.  
  130.    close_window();
  131.    return TRUE;
  132. }
  133.  
  134.  
  135. static void set_level( void )
  136. {
  137.    int level;
  138.  
  139.    level = ire( rgb[ 0 ], rgb[ 1 ], rgb[ 2 ] );
  140.    GT_SetGadgetAttrs( gad[ 3 ], wnd, NULL,
  141.       GTNM_Number, level,
  142.       TAG_DONE );
  143.    SetRGB4( &wnd->WScreen->ViewPort, 3,
  144.       rgb[ 0 ] >> 4,
  145.       rgb[ 1 ] >> 4,
  146.       rgb[ 2 ] >> 4 );
  147. }
  148.  
  149.  
  150. static BOOL open_window( void )
  151. {
  152.    char *gtext[ 3 ] = { "R", "G", "B" };
  153.    struct NewGadget ng;
  154.    struct Gadget *g;
  155.    struct Screen *s;
  156.    int i;
  157.  
  158.    s = LockPubScreen( NULL );
  159.    if ( !s ) return FALSE;
  160.    visinfo = GetVisualInfo( s, TAG_DONE );
  161.    top = s->WBorTop + s->Font->ta_YSize + 1;
  162.    i = GetRGB4( s->ViewPort.ColorMap, 3 );
  163.    entry3[ 0 ] = ( i & 0xF00 ) >> 8,
  164.    entry3[ 1 ] = ( i & 0x0F0 ) >> 4,
  165.    entry3[ 2 ] = i & 0x00F;
  166.  
  167.    g = CreateContext( &glist );
  168.  
  169.    ng.ng_LeftEdge   = 45;
  170.    ng.ng_Width      = 261;
  171.    ng.ng_Height     = 14;
  172.    ng.ng_TextAttr   = &myfont;
  173.    ng.ng_Flags      = PLACETEXT_RIGHT;
  174.    ng.ng_VisualInfo = visinfo;
  175.    ng.ng_UserData   = NULL;
  176.  
  177.    for ( i = 0; i < 3; i++ ) {
  178.       rgb[ i ] = entry3[ i ] * 17;
  179.       ng.ng_TopEdge    = top + 34 + i * 18;
  180.       ng.ng_GadgetText = gtext[ i ];
  181.       ng.ng_GadgetID   = i + 1;
  182.  
  183.       gad[ i ] = g = CreateGadget( SLIDER_KIND, g, &ng,
  184.          GTSL_Min,         0,
  185.          GTSL_Max,         255,
  186.          GTSL_Level,       rgb[ i ],
  187.          GTSL_LevelFormat, "%3ld",
  188.          GTSL_MaxLevelLen, 3,
  189.          GTSL_LevelPlace,  PLACETEXT_LEFT,
  190.          PGA_Freedom,      LORIENT_HORIZ,
  191.          TAG_DONE );
  192.    }
  193.  
  194.    i = ire( rgb[ 0 ], rgb[ 1 ], rgb[ 2 ] );
  195.  
  196.    ng.ng_LeftEdge   = 142;
  197.    ng.ng_TopEdge    = top + 14;
  198.    ng.ng_Width      = 24;
  199.    ng.ng_Height     = 8;
  200.    ng.ng_GadgetText = "IRE";
  201.    ng.ng_GadgetID   = 4;
  202.    ng.ng_Flags      = PLACETEXT_RIGHT;
  203.  
  204.     gad[ 3 ] = g = CreateGadget( NUMBER_KIND, g, &ng,
  205.       GTNM_Number, i,
  206.       TAG_DONE );
  207.  
  208.    if ( !g ) {
  209.       close_window();
  210.       return FALSE;
  211.    }
  212.  
  213.     wnd = OpenWindowTags( NULL,
  214.       WA_Left,         160,
  215.       WA_Top,          75,
  216.       WA_Width,        340,
  217.       WA_Height,       110,
  218.       WA_IDCMP,
  219.          SLIDERIDCMP       |
  220.          IDCMP_CLOSEWINDOW |
  221.          IDCMP_REFRESHWINDOW,
  222.       WA_Flags,
  223.          WFLG_DRAGBAR        |
  224.          WFLG_CLOSEGADGET    |
  225.          WFLG_DEPTHGADGET    |
  226.          WFLG_SIMPLE_REFRESH |
  227.          WFLG_ACTIVATE,
  228.       WA_Gadgets,   glist,
  229.       WA_Title,     "RGB to IRE",
  230.       WA_PubScreen, s,
  231.       TAG_DONE );
  232.  
  233.    if ( !wnd ) {
  234.       close_window();
  235.       return FALSE;
  236.    }
  237.  
  238.    UnlockPubScreen( NULL, s );
  239.     GT_RefreshWindow( wnd, NULL );
  240.    draw_box();
  241.    return TRUE;
  242. }
  243.  
  244.  
  245. static void draw_box( void )
  246. {
  247.    static struct Image box = { 0, 0, 80, 20, 2, NULL, 0, 3, NULL };
  248.  
  249.    DrawImage( wnd->RPort, &box, 47, top + 7 );
  250.    DrawBevelBox( wnd->RPort, 45, top + 6, 84, 22,
  251.       GT_VisualInfo, visinfo,
  252.       GTBB_Recessed, TRUE,
  253.       TAG_DONE );
  254. }
  255.  
  256.  
  257. static void close_window( void )
  258. {
  259.     if ( wnd ) {
  260.       SetRGB4( &wnd->WScreen->ViewPort, 3,
  261.          entry3[ 0 ], entry3[ 1 ], entry3[ 2 ] );
  262.       CloseWindow( wnd );
  263.    }
  264.     if ( glist )   FreeGadgets( glist );
  265.    if ( visinfo ) FreeVisualInfo( visinfo );
  266. }
  267.